using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Chat.Util;


namespace Chat.Client
{
    class MessageSend
    {
        public static byte[] getSendMsg(User u,string msg)
        {
            byte[] bMsg = Tools.getStringBytes(msg);
            Head h = new Head();
            h.flag = Config.C_SEND;
            h.id = u.id;
            h.name = u.name;
            h.len = bMsg.Length;
            byte[] bHead = Tools.getHeadBytes(h);
            byte[] data = Tools.appendBytes(bHead, bMsg);
            return data;
        }

        public static byte[] getForwardMsg(User fu, string toId,string msg)
        {
            byte[] bMsg = Tools.getStringBytes(msg);
            byte[] bId = new byte[Config.ID_SIZE];
            byte[] id = Tools.getStringBytes(toId);
            Tools.addBytes(id, bId, 0);
            Head h = new Head();
            h.flag = Config.C_FORWARD;
            h.id = fu.id;
            h.name = fu.name;
            h.ip = fu.ip;
            h.len = bMsg.Length + Config.ID_SIZE;

            byte[] bHead = Tools.getHeadBytes(h);
            byte[] bBody = Tools.appendBytes(bId, bMsg);
            byte[] data = Tools.appendBytes(bHead, bBody);
            return data;
        }

        public static byte[] getLoginMsg(string id,string name)
        {
            Head h = new Head();
            h.flag = Config.C_LOGIN;
            h.id = id;
            h.name = name;
            h.len = 0;
            byte[] bHead = Tools.getHeadBytes(h);
            return bHead;
        }

        public static byte[] getLoginMsg(string id)
        {
            return getLoginMsg(id, id);
        }

        public static byte[] getLogoutMsg(string id)
        {
            Head h = new Head();
            h.flag = Config.C_EXIT;
            h.id = id;
            h.name = "";
            h.len = 0;
            byte[] bHead = Tools.getHeadBytes(h);
            return bHead;
        }

        public static byte[] getUserListMsg(User u)
        {
            Head h = new Head();
            h.flag = Config.C_USERLIST;
            h.id = u.id;
            h.name = u.name;
            h.len = 0;
            byte[] bHead = Tools.getHeadBytes(h);
            return bHead;

        }

    }
}
